//
//  LandlordHomeViewController.m
//  YJRenter
//
//  Created by 文彬 on 2018/7/2.
//  Copyright © 2018年 文彬. All rights reserved.
//

#import "LandlordHomeViewController.h"
#import "HouseDetailNavView.h"
#import "LandlordHomeHeaderView.h"
#import "HouseInfoTableViewCell.h"
#import "HouseMoneyTableViewCell.h"
#import "InputItemTableViewCell.h"
#import "CommentUserInfoTableViewCell.h"
#import "UILabel+ChangeLineSpaceAndWordSpace.h"
#import "CommentContentTableViewCell.h"
#import "CommentPictureTableViewCell.h"
#import "RoomCommentsViewController.h"

@interface LandlordHomeViewController ()<UITableViewDelegate,
    UITableViewDataSource,
    HouseDetailNavViewDelegate>

@property (strong, nonatomic)UITableView *listTableView;
@property (strong, nonatomic)HouseDetailNavView *navView;
@property (strong, nonatomic)LandlordHomeHeaderView *headView;

@end

@implementation LandlordHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initPageControl];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear: animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark 页面相关
- (void)initPageControl{
    [self.view addSubview:self.listTableView];
    [self.listTableView makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(@0);
    }];
    self.listTableView.tableHeaderView = self.headView;
    
    [self.view addSubview:self.navView];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark HouseFilterViewDelegate
- (void)houseDetailNavClick:(NSInteger)index{
    if (index==10) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark ScrollView
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    [self.navView scrollOffset:scrollView.contentOffset.y];
}

#pragma mark UITableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2+4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==1) {
        return 2;
    }
    else if(section>=2){
        return 2;
    }
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return section>2?0.1 :50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        if (indexPath.row==0) {
            return 60;
        }
        else if(indexPath.row==1){
            return 110;
        }
        else if(indexPath.row==2){
            return 100;
        }
        return 70;
    }
    else if(indexPath.section==1){
        return 50;
    }
    else if(indexPath.section>=2){
        if (indexPath.row==0) {
            return 200;
        }
        return 60;
    }
    return 150;
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section>2) {
        return nil;
    }
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.5)];
    lineView.backgroundColor = kLineColor;
    [view addSubview:lineView];
    
    view.backgroundColor = [UIColor whiteColor];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(15, 15, 100, 30)];
    label.font = [UIFont systemFontOfSize:15];
    if (section==0) {
        label.text = @"租客点评";
    }
    else if(section==1){
        label.text = @"房东认证";
    }
    else {
        label.text = @"其他房源";
    }
    [view addSubview:label];
    
    return view;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section>=2) {
        
        if(indexPath.row==0){
            
            HouseInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HouseInfoTableViewCell" forIndexPath:indexPath];
            [cell.houseImgView sd_setImageWithURL:[NSURL URLWithString:@"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=4036889747,3526425271&fm=27&gp=0.jpg"] placeholderImage:nil];
            [cell.headImgView setRoundImageWithURL:@"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=4036889747,3526425271&fm=27&gp=0.jpg" placeholderImage:nil];
            return cell;
        }
        
        HouseMoneyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HouseMoneyTableViewCell" forIndexPath:indexPath];
        return cell;
    }
    else if(indexPath.section==1){
        
        InputItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"InputItemTableViewCell" forIndexPath:indexPath];
        cell.inputTxtField.enabled = NO;
        if (indexPath.row==0) {
            cell.titleLabl.text = @"实名认证";
            cell.inputTxtField.text = @"已验证";
        }
        else {
            cell.titleLabl.text = @"手机验证";
            cell.inputTxtField.text = @"已验证";
        }
        return cell;
    }
    
    if (indexPath.row==0) {
        CommentUserInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CommentUserInfoTableViewCell" forIndexPath:indexPath];
        [cell.imgView setRoundImageWithURL:@"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=4036889747,3526425271&fm=27&gp=0.jpg" placeholderImage:nil];
        return cell;
    }
    else if(indexPath.row==1){
       
        CommentContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CommentContentTableViewCell" forIndexPath:indexPath];
        cell.contentLabel.text = @"房间很赶紧，非常不错，房间很赶紧，非常不错，房间很赶紧，非常不错，房间很赶紧，非常不错，房间很赶紧，非常不错";
        [UILabel changeLineSpaceForLabel:cell.contentLabel WithSpace:5];
        cell.allBtn.hidden = YES;
        
        return cell;
    }
    else if(indexPath.row==2){
        
        CommentPictureTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CommentPictureTableViewCell" forIndexPath:indexPath];
        for (int i=0; i<3; i++) {
            UIImageView *imgView = [cell.contentView viewWithTag:i+100];
            [imgView sd_setImageWithURL:[NSURL URLWithString:@"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=4036889747,3526425271&fm=27&gp=0.jpg"] placeholderImage:nil];
        }
        return cell;
    }
    
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"iden"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"查看全部评价" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:16];
    [button setTitleColor:kTitleColor forState:UIControlStateNormal];
    button.layer.cornerRadius = 3;
    button.layer.borderColor = kMainOriginColor.CGColor;
    button.layer.borderWidth = 0.5;
    [cell.contentView addSubview:button];
    [button makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@150);
        make.height.equalTo(@40);
        make.centerX.equalTo(cell.contentView.centerX);
        make.centerY.equalTo(cell.contentView.centerY);
    }];
    [button buttonClick:^{
        RoomCommentsViewController *commentsController = [[RoomCommentsViewController alloc]init];
        [self.navigationController pushViewController:commentsController animated:YES];
    }];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

#pragma mark getter
- (UITableView*)listTableView{
    if (_listTableView==nil) {
        _listTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _listTableView.delegate = self;
        _listTableView.dataSource = self;
        _listTableView.sectionFooterHeight = 0;
        _listTableView.backgroundColor = [UIColor clearColor];
        _listTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_listTableView registerNib:[UINib nibWithNibName:@"HouseMoneyTableViewCell" bundle:nil] forCellReuseIdentifier:@"HouseMoneyTableViewCell"];
        [_listTableView registerNib:[UINib nibWithNibName:@"HouseInfoTableViewCell" bundle:nil] forCellReuseIdentifier:@"HouseInfoTableViewCell"];
        [_listTableView registerNib:[UINib nibWithNibName:@"InputItemTableViewCell" bundle:nil] forCellReuseIdentifier:@"InputItemTableViewCell"];
        [_listTableView registerNib:[UINib nibWithNibName:@"CommentUserInfoTableViewCell" bundle:nil] forCellReuseIdentifier:@"CommentUserInfoTableViewCell"];
        [_listTableView registerNib:[UINib nibWithNibName:@"CommentContentTableViewCell" bundle:nil] forCellReuseIdentifier:@"CommentContentTableViewCell"];
        [_listTableView registerNib:[UINib nibWithNibName:@"CommentPictureTableViewCell" bundle:nil] forCellReuseIdentifier:@"CommentPictureTableViewCell"];
        
        
    }
    
    return _listTableView;
}

- (HouseDetailNavView*)navView{
    if (_navView==nil) {
        _navView = [[HouseDetailNavView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
        _navView.delegate = self;
    }
    return _navView;
}

-(LandlordHomeHeaderView*)headView{
    if (_headView==nil) {
        
        _headView = [[LandlordHomeHeaderView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 290)];
    }
    return _headView;
}
@end
